<?php

namespace app\admin\controller;
use app\common\model\Role;
use think\Controller;
use think\Request;
use app\common\lib\IAuth;
class Admin extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $role = Role::select();
        $this->assign('role', $role);
        $where = [];
        $keyword = input('name');
        $role_id=input('role_id');
        if(!empty($keyword)){
            $where['username'] = ['like', "%$keyword%"];
        }if(!empty($role_id)){
        $where['role_id'] = intval($role_id);
    }
        $list = model('AdminUser')::with('role')->where($where)->paginate(5);
        $this->assign('list', $list);
        return view('admin');
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $role = Role::select();
        $this->assign('role', $role);
       return $this->fetch('add');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $data=input('post.');
        $validate = $this->validate($data, [
            'username|管理员账号' => 'require|unique:admin_user',
            'password|初始密码' => 'require',
            'realname|昵称' => 'require',
            'role_id|角色' => 'require|integer|gt:0'
        ]);
        if($validate !== true){
            $this->error($validate,'index');

        }
            $insertData=[
                'username'=>$data['username'],
                'password'=>IAuth::setPassword($data['password']),
                'role_id'=>$data['role_id'],
                'realname'=>$data['realname']
            ];

            $id=model('AdminUser')->save($insertData);
            if($id)
            {
                $this->success('添加成功','index');
            }else{
                $this->error('添加失败','index');
            }



    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $role = Role::select();
        $this->assign('role', $role);
        $info = model('AdminUser')->find($id);

        $this->assign('info', $info);
        return view('edit');
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $data=input('post.');
        $validate = $this->validate($data, [
            'username|管理员账号' => 'require|unique:admin_user',
            'password|密码' => 'require',
            'realname|昵称' => 'require',
            'role_id|角色' => 'require|integer|gt:0'
        ]);
        if($validate !== true){
            $this->error($validate,'index');

        }
        $editData=[
            'username'=>$data['username'],
            'password'=>IAuth::setPassword($data['password']),
            'role_id'=>$data['role_id'],
            'realname'=>$data['realname']
        ];
        unset($data['username']);
        unset($data['password']);
        Model('AdminUser')::update($editData,['id' => $id], true);
        return $this->success('修改成功','index');

    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $admin=model('AdminUser')::find($id);
        model('AdminUser')->delete($admin);
        return $this->success('删除成功','index');
    }
}
